﻿using PersistenceLayer.DAL;
using SupportLayer.Constantes;
using SupportLayer.Constantes.CLIENTE;
using SupportLayer.Constantes.OUTROS;
using SupportLayer.Objetos.Cliente;
using SupportLayer.Objetos.Usuario;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI.WebControls;
using Uol.PagSeguro.Domain;
using Util;

namespace ByBikeShop.Site
{
	public abstract class BasePage : System.Web.UI.Page
	{
		public AccountCredentials credentials = new AccountCredentials("jtsbikepartsltda@hotmail.com", "F17C7E538C154917B602C0DE10BA9EA0");

		public PersistenceLayer.Model.Cliente Cli { get; set; }

		public void PageLoad()
		{
			CarregaUsuario();
			CarregaDados();
		}

		private void CarregaUsuario()
		{
			try
			{
				PlaceHolder placeUsuario = new PlaceHolder();
				ImageButton btnLogout = new ImageButton();
				btnLogout.Visible = false;
				placeUsuario = (PlaceHolder)this.Page.Master.FindControl("placeLogar");
				HyperLink hplUsuario = new HyperLink();
				hplUsuario.CssClass = "linksGreen";

				if (RecuperaCookie(SupportLayer.Constantes.CLIENTE.CLIENTE.ID) == null)
				{
					hplUsuario.Text = "login";
					hplUsuario.NavigateUrl = "/Cliente/Pages/Login.aspx";
					placeUsuario.Controls.Add(hplUsuario);
				}
				else
				{
					if (!RecuperaCookie(SupportLayer.Constantes.CLIENTE.CLIENTE.ID).Equals(string.Empty))
						Cli = new ClienteDAL().Obter(Convert.ToInt64(Criptografia.Decriptar(SupportLayer.Constantes.CRIPTO.CHAVE, SupportLayer.Constantes.CRIPTO.VETOR, RecuperaCookie(CLIENTE.ID))));
					//Cliente = BusinessCliente.SelectCliente(Convert.ToInt64(Criptografia.Decriptar(CRIPTO.CHAVE, CRIPTO.VETOR, RecuperaCookie(CLIENTE.ID))));
					else
						Cli = null;

					if ((Cli == null) || (Cli.ID <= 0))
					{
						hplUsuario.Text = "login";
						hplUsuario.NavigateUrl = "/Cliente/Pages/Login.aspx";
					}
					else
					{
						hplUsuario.Text = "bem vindo, " + Cli.Nome.ToLower() + "!";
						hplUsuario.NavigateUrl = "/Cliente/Pages/Home.aspx";

						btnLogout.Visible = true;
						btnLogout.Style.Add("margin-left", "20px");
						btnLogout.ImageUrl = "/Imagens/Icones/logout.png";
						btnLogout.Height = 20;
						btnLogout.Click += btnLogout_Click;
					}
					placeUsuario.Controls.Add(hplUsuario);
					placeUsuario.Controls.Add(btnLogout);
				}
			}
			catch (Exception)
			{
				throw;
			}
		}
		public abstract void CarregaDados();

		public decimal CalcularDesconto(decimal preco, int desconto)
		{
			return preco - ((preco * desconto) / 100);
		}

		#region Cookies
		public void CreateCookie(string Name, string Value, int hoursToExpire)
		{
			HttpCookie cookie = new HttpCookie(Name);
			cookie.Value = Value;
			cookie.Expires = DateTime.Now.AddHours(hoursToExpire);
			Response.Cookies.Add(cookie);
		}
		public string RecuperaCookie(string Name)
		{
			HttpCookie cookie = Request.Cookies[Name];
			if (cookie != null)
				return cookie.Value;
			else
				return string.Empty;
		}
		#endregion

		public void RedirectProdutos(int page = 1, string filter = "", int ordenacao = 1)
		{
			if (filter != "")
				Response.Redirect("/Site/Pages/Produtos.aspx?" + OUTROS.PAGINA.ToString() + "=" +
					Util.Criptografia.Encriptar(SupportLayer.Constantes.CRIPTO.CHAVE, SupportLayer.Constantes.CRIPTO.VETOR, page.ToString()) +
					"&" + OUTROS.FILTRO + "=" + Util.Criptografia.Encriptar(SupportLayer.Constantes.CRIPTO.CHAVE, SupportLayer.Constantes.CRIPTO.VETOR, filter) +
					"&" + OUTROS.ORDENACAO + "=" + Util.Criptografia.Encriptar(SupportLayer.Constantes.CRIPTO.CHAVE, SupportLayer.Constantes.CRIPTO.VETOR, ordenacao.ToString()));
			else
				Response.Redirect("/Site/Pages/Produtos.aspx?" + OUTROS.PAGINA.ToString() + "=" +
					Util.Criptografia.Encriptar(SupportLayer.Constantes.CRIPTO.CHAVE, SupportLayer.Constantes.CRIPTO.VETOR, page.ToString()) +
					"&" + OUTROS.ORDENACAO + "=" + Util.Criptografia.Encriptar(SupportLayer.Constantes.CRIPTO.CHAVE, SupportLayer.Constantes.CRIPTO.VETOR, ordenacao.ToString()));
		}
		//public void RedirectFacebook(string _currentPage, string _caption, string _description, string _imageUrl, string _message, string _nome)
		//{
		//	CreateCookie(OUTROS.CURRENT_PAGE, _currentPage, 1);
		//	Session[OUTROS.CURRENT_PAGE] = _currentPage;

		//	BusinessLayer.Face face = new BusinessLayer.Face(_currentPage);
		//	var user = face.RequestPermissions(Request, Response);
		//	face.PostLinkToWall(user, _caption, _description, _imageUrl, _message, _nome, "Facebook_Post.aspx");
		//}

		void btnLogout_Click(object sender, System.Web.UI.ImageClickEventArgs e)
		{
			HttpCookie cookie = Request.Cookies[CLIENTE.ID];
			if (cookie != null)
				cookie.Expires = DateTime.Now.AddHours(-1);

			Response.Cookies.Add(cookie);

			Response.Redirect("/Site/Pages/Home.aspx");

		}

	}
}